package com.lungle.warmdiary.app.view.active.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.base.BaseViewPagerAdapter;
import com.lungle.warmdiary.app.model.event.ActiveStatueRefreshEvent;
import com.lungle.warmdiary.app.ui.tab.SlidingTabLayout;
import com.lungle.warmdiary.app.utils.LogUtils;
import com.lungle.warmdiary.app.view.IPagerFragment;
import com.lungle.warmdiary.app.view.active.adapter.ActiveTabPagerAdapter;

import org.greenrobot.eventbus.EventBus;

public class ActivePagerFragment extends Fragment implements
        IPagerFragment {

    private ViewPager mViewPager;
    private ActiveTabPagerAdapter mTabAdapter;
    private SlidingTabLayout mSlidingTabLayout;
    private int tab;
    private MediaReceiver mMediaReceiver = new MediaReceiver();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter();
        filter.addAction(AppConstant.INTENT_ACTION_TAB_JUMP);
        getActivity().registerReceiver(mMediaReceiver, filter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.v2_fragment_viewpager2, container,
                false);

        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.tab_indicator, R.id.tv_media_name);
        Resources res = getResources();
        int color = res.getColor(R.color.main_red);
        mSlidingTabLayout.setSelectedIndicatorColors(color);
        mSlidingTabLayout.setDistributeEvenly(true);

        mViewPager = (ViewPager) view.findViewById(R.id.main_tab_pager);
        mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.view_pager_margin));


        if (mTabAdapter == null) {
            mTabAdapter = new ActiveTabPagerAdapter(getChildFragmentManager());
        }
        //缓存数目改成4 2016-09-24
//        mViewPager.setOffscreenPageLimit(4);
        mViewPager.setOffscreenPageLimit(mTabAdapter.getCount() - 1);
        mViewPager.setAdapter(mTabAdapter);

        mSlidingTabLayout.setViewPager(mViewPager);
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(AppConstant.INTENT_ACTION_TAB_JUMP);
//        getActivity().registerReceiver(mMediaReceiver, filter);

        return view;
    }


    @Override
    public BaseViewPagerAdapter getPagerAdapter() {
        return mTabAdapter;
    }

    @Override
    public ViewPager getViewPager() {
        return mViewPager;
    }

    @Override
    public Fragment getCurrentFragment() {
        return mTabAdapter.getItemAt(mViewPager.getCurrentItem());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMediaReceiver != null && getActivity() != null)
            getActivity().unregisterReceiver(mMediaReceiver);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private class MediaReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            tab = intent.getIntExtra("tabid", -1);
            LogUtils.printLogE("tabid: " + tab);
            if (tab >= 0 && mViewPager != null) {
                mViewPager.setCurrentItem(tab);
                EventBus.getDefault().post(new ActiveStatueRefreshEvent(0));
            }
        }
    }
}